<template>
  <div class="login">
    <!-- 导航部分 -->
      <van-nav-bar
      title="标题"
      class="page-nav-bar"
      >
        <template #left>
            <van-icon name="cross" color="#fff" @click="$router.back()"/>
        </template>
      </van-nav-bar>
    <!-- 表单部分 -->
    <van-form @submit="onSubmit" ref="form">
        <van-field
          v-model="form.mobile"
          name='mobile'
          autocomplete ='false'
          placeholder="请输入手机号"
          :rules="[
          {
            required:true, message: '请输入手机号'
          },
          {
            pattern:/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/ ,message:'请输入正确的手机号'
          }

          ]"

        >
            <template #left-icon>
                <i class="toutiao toutiao-shouji"></i>
            </template>
        </van-field>
        <van-field
          autocomplete ='false'
          v-model="form.code"
          name="code"
          type="password"
          placeholder="请输入验证码"
          :rules="[
          {
            required:true, message: '请输入验证码'
          },
          {
            pattern:/^[0-9]{6}$/ ,message:'请输入正确的验证码'
          }

          ]"
        >
            <template #left-icon>
              <i class="toutiao toutiao-yanzhengma"></i>
            </template>
             <template #button>
              <van-count-down @finish="ishow=false" v-if="ishow" :time="60* 1000" format=" ss s" />
              <van-button v-else @click="sendcode" native-type="button" class="btn-send" size="small" type="primary">发送验证码</van-button>
            </template>
        </van-field>
        <div style="margin: 16px;">
          <van-button class="login-btn" round block type="info" native-type="submit">提交</van-button>
        </div>
    </van-form>
  </div>
</template>

<script>
import { getverifycode, login } from '@/api/getverify'
import { Toast } from 'vant'
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      ishow: false
    }
  },

  created () {

  },

  methods: {
    async onSubmit () {
      try {
        const res = await login(this.form)
        this.$store.commit('inituser', res)
        this.$router.push('/profile')
      } catch (error) {
        Toast.fail('登陆失败')
      }
    },
    async  sendcode () {
      try {
        await this.$refs.form.validate('mobile')
        await getverifycode(this.form.mobile)
        this.ishow = true
        Toast.success('发送成功')
      } catch (error) {
        if (!error.response) return
        if (error.response.status === 429) {
          return Toast.success('亲，发送频繁')
        }
        Toast.fail('网络异常')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.login {
  .login-btn {
    width: 694px;
    height: 88px;
    background: #6DB4FB;
    border-radius: 10px;
    border: none;
  }
  .toutiao{
    font-size: 37px;
  }
  .btn-send{
    width: 152px;
    height: 46px;
    background: #EDEDED;
    border: none;
    border-radius: 23px;
    padding: 0;
    font-size: 22px;
    color: #666666;
  }
}
</style>
